package com.strongwell.awe.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.strongwell.awe.model.TbMenus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import com.strongwell.awe.constant.Constants;
import com.strongwell.awe.util.Query;
import com.strongwell.awe.util.ResultUtil;
import com.strongwell.awe.base.controller.BaseController;
import com.strongwell.awe.annotation.SysLog;
import com.strongwell.awe.model.CmsCategoryEntity;
import com.strongwell.awe.service.CmsCategoryService;

/**
 * 栏目表
 * 
 * @author bridgezn
 * @email 39440421@qq.com
 * @date 2018-09-13 14:40:31
 */
@Controller
@RequestMapping("cmscategory")
public class CmsCategoryController extends BaseController{
	@Autowired
	private CmsCategoryService cmsCategoryServiceImpl;
	
	/**
	 * 页面
	 */
	@RequestMapping("/page")
	public String page(@RequestParam Map<String, Object> params){
		return "page/cmscategory/categoryList";
	}
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(@RequestParam Map<String, Object> params){
	    Query query = new Query(params);
		List<CmsCategoryEntity> cmsCategoryList = cmsCategoryServiceImpl.queryList(query);
		int total = cmsCategoryServiceImpl.queryTotal(query);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",total,cmsCategoryList);
	}

	/**
	 * 获取菜单信息
	 * @param //menu
	 * @return
	 */
	@RequestMapping("/data")
	//@RequiresPermissions("sys:menu:list")
	@ResponseBody
	public ResultUtil categoryData(){
		List<CmsCategoryEntity> list=cmsCategoryServiceImpl.selCategoryByParentId();

		return ResultUtil.ok(list);
	}
	/**
	 * 信息
	 */
	@RequestMapping(value="/info/{id}", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object info(@PathVariable("id") Long id){
		CmsCategoryEntity cmsCategory = cmsCategoryServiceImpl.queryObject(id);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",0,cmsCategory);
	}

    /**
     * 增加页面
     */
    @RequestMapping("addcmscategory")
    public String cmscategoryAdd(){
        return "page/cmscategory/addcmscategory";
    }

	@RequestMapping("addcmscategory/{pid}")
	public String Addcmscategory(HttpServletRequest req,@PathVariable("pid")String pid){
		req.setAttribute("parentId",pid);
		return "page/cmscategory/addcmscategory";
	}

    @RequestMapping("/checkCmsCategoryName/{name}")
    @ResponseBody
    public ResultUtil checkCmsCategoryName(@PathVariable("name")String name) {
            CmsCategoryEntity cmscategorys = cmsCategoryServiceImpl.queryName(name);
        if(cmscategorys!=null){
            return new ResultUtil(500,"名称已存在！");
        }
        return new ResultUtil(0);
    }
	/**
	 * 保存
	 */
	@RequestMapping(value="/save", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object save(@ModelAttribute CmsCategoryEntity cmsCategory){
		cmsCategoryServiceImpl.save(cmsCategory);
		return putMsgToJsonString(Constants.WebSite.SUCCESS,"",0,cmsCategory);
	}

    /**
     * 修改待信息显示
     */

    @RequestMapping(value="/editcmscategory/{id}")
    //@RequiresPermissions("cmscategory:cmscategory:update")
    public String editcmssite(HttpServletRequest req, @PathVariable("id")Long id) {
            CmsCategoryEntity cmscategorys = cmsCategoryServiceImpl.queryObject(id);

        req.setAttribute("cmscategory",cmscategorys);

        return "page/cmscategory/editcmscategory";
    }
	/**
	 * 修改
	 */
	@RequestMapping(value="/update", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object update(@ModelAttribute CmsCategoryEntity cmsCategory){
		int result = cmsCategoryServiceImpl.update(cmsCategory);
		return putMsgToJsonString(result,"",0,"");
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete", produces="text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public Object delete(@RequestParam Long[] ids){
		int result = cmsCategoryServiceImpl.deleteBatch(ids);
		return putMsgToJsonString(result,"",0,"");
	}

    @SysLog(value="删除文章信息")
    @RequestMapping("/delById/{id}")
    //@RequiresPermissions("cmscategory:cmscategory:delete")
    @ResponseBody
    public ResultUtil delById(@PathVariable("id")Long id) {

        try {
                cmsCategoryServiceImpl.delById(id);
            return ResultUtil.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    /**
     * 批量删除指定管理员
     * @param //id
     * @return
     */
    @SysLog(value="批量删除文章信息")
    @RequestMapping("/delPatch/{Str}")
    //@RequiresPermissions("cmscategory:cmscategory:delete")
    @ResponseBody
    public ResultUtil delPatch(@PathVariable("Str")String cmscategoryStr) {

        try {
                cmsCategoryServiceImpl.delPatch(cmscategoryStr);
            return ResultUtil.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }

    }
}
